﻿Public Class clsMessage1
    Dim webMessage As WebBrowser
    Dim webMessageComposer As WebBrowser
    Dim parseHTML As New clsParseHTML
    Dim util As New clsUtil
    Dim is_composer As Boolean = False
    Public Sub New()
        webMessage = New WebBrowser
        webMessageComposer = New WebBrowser
        AddHandler webMessage.DocumentCompleted, AddressOf webMessage_DocumentCompleted
        AddHandler webMessageComposer.DocumentCompleted, AddressOf webMessageComposer_DocumentCompleted
    End Sub

    Private Sub webMessage_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        If webMessage.ReadyState = WebBrowserReadyState.Complete Then
            HandleWebMessage()

        End If
    End Sub

    Private Sub webMessageComposer_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        If webMessageComposer.ReadyState = WebBrowserReadyState.Complete Then
            HandleWebMessageComposer()

        End If
    End Sub

    Public Sub getMessageUnread()
        webMessage.Navigate("http://m.facebook.com/messages/?folder=unread")
    End Sub

    Public Sub sendMessage(ByVal url As String)
        is_composer = True
        webMessageComposer.Navigate(url)
    End Sub

    Public Sub HandleWebMessageComposer()
        If (webMessageComposer.Document IsNot Nothing) Then
            If (webMessageComposer.Document.GetElementsByTagName("textarea").GetElementsByName("body").Count > 0) Then
                If (is_composer = True) Then
                    webMessageComposer.Document.GetElementsByTagName("textarea").GetElementsByName("body").Item(0).InnerText = "noi dung gui di"
                    webMessageComposer.Document.GetElementById("composer_form").InvokeMember("submit")
                    is_composer = False
                End If
            End If
        End If
    End Sub
    Public Sub HandleWebMessage()
        If (webMessage.Document IsNot Nothing) Then
            Dim arrMessage As ArrayList
            arrMessage = parseHTML.getByClassname(webMessage.Document.Body.InnerHtml, "h3", "bo bh bp")
            Dim arrMessageDetail As ArrayList
            arrMessageDetail = parseHTML.getByClassname(webMessage.Document.Body.InnerHtml, "h3", "bq br bs")
            If (arrMessage.Count > 0) Then
                Dim i As Integer
                For i = 0 To arrMessage.Count - 1
                    Dim href As New ArrayList
                    href = parseHTML.getByAttribute(arrMessage(i), "a", "href")
                    Dim name As New ArrayList
                    name = parseHTML.getByAttribute(arrMessage(i), "a", "innerText")
                    Dim text As New ArrayList
                    text = parseHTML.getByAttribute(arrMessageDetail(i), "strong", "innerText")
                    If (href.Count > 0) Then
                        Dim j As Integer
                        Dim is_add As Boolean = True
                        For j = 0 To frmMain.lstMessageUnread.Items.Count - 1
                            If (frmMain.lstMessageUnread.Items(j).SubItems(0).Text = util.format_fullname_facebook(name(0))) Then
                                is_add = False
                            End If
                        Next
                        If is_add = True Then
                            With frmMain.lstMessageUnread.Items.Add(util.format_fullname_facebook(name(0)))
                                .SubItems.Add(text(0))
                                .SubItems.Add(util.remove_about_blank(href(0)))
                            End With
                        End If
                    End If
                Next
            End If
        End If
    End Sub
End Class
